import json
import redis


class RedisClient:
    def __init__(self, redis_url, expire_time=3600):
        self.client = redis.StrictRedis.from_url(redis_url, decode_responses=True)
        self.expire_time = expire_time
        # 清空所有数据
        self.client.flushall()

    def save_conversation_history(self, conversation_turn, chat_id):
        all_history = self.client.get('conversation_turns')
        if all_history:
            all_history = json.loads(all_history)
        else:
            all_history = {}

        # 将 conversation_turn 存储到与 chat_id 相关的对话历史中
        all_history[chat_id] = conversation_turn

        # 将更新后的对话历史保存到 Redis
        self.client.set('conversation_turns', json.dumps(all_history))
        self.client.expire('conversation_turns', self.expire_time)  # 设置过期时间

    def get_conversation_history_by_id(self, chat_id):
        all_history = self.client.get('conversation_turns')
        if all_history:
            all_history = json.loads(all_history)
            # 获取与给定 chat_id 相关的对话历史
            if chat_id in all_history:
                return all_history[chat_id]
            else:
                return []
        else:
            return []

    def get_conversation_history(self):
        all_history = self.client.get('conversation_turns')
        if all_history:
            return json.loads(all_history)
        return {}

    def save_chat_info(self, chat_id, chat_text):
        chat_info = self.client.get('chat_info')
        if chat_info:
            chat_info = json.loads(chat_info)
        else:
            chat_info = {}

        chat_info[chat_id] = chat_text
        self.client.set('chat_info', json.dumps(chat_info))
        self.client.expire('chat_info', self.expire_time)

    def get_chat_info(self):
        chat_info = self.client.get('chat_info')
        if chat_info:
            chat_info = json.loads(chat_info)
            return chat_info
        else:
            return []

    def delete_chat_info(self, chat_id):
        # 获取聊天信息和对话历史
        chat_infos = self.client.get('chat_info')
        conversation_turns = self.client.get('conversation_turns')

        if chat_infos and conversation_turns:
            chat_info = json.loads(chat_infos)
            conversation_turn = json.loads(conversation_turns)

            # 从 chat_info 中删除指定的 chat_id
            if chat_id in chat_info:
                del chat_info[chat_id]
            else:
                print("chat_id不存在")
                print(chat_id)
                print(chat_info)

            # 从 conversation_turns 中删除指定的 chat_id
            if chat_id in conversation_turn:
                del conversation_turn[chat_id]

            # 更新存储中的聊天信息和对话历史
            self.client.set('chat_info', json.dumps(chat_info))
            self.client.set('conversation_turns', json.dumps(conversation_turn))
        else:
            pass  # 处理无聊天信息或对话历史的情况

    def get_current_chat_id(self):
        current_chat_id = self.client.get('current_chat_id')
        if current_chat_id:
            current_chat_id = json.loads(current_chat_id)
            return current_chat_id
        else:
            return []

    def save_current_chat_id(self, chat_id):
        current_chat_id = self.client.get('current_chat_id')
        if current_chat_id:
            current_chat_id = json.loads(current_chat_id)
        else:
            current_chat_id = {}

        # 将更新后的当前对话id保存到 Redis
        current_chat_id['chat_id'] = chat_id
        self.client.set('current_chat_id', json.dumps(current_chat_id))
        self.client.expire('current_chat_id', self.expire_time)  # 设置过期时间

    def generate_chat_id(self):
        # 获取当前最大id
        current_max = self.client.get('max_chat_id')
        if current_max is None:
            new_id = 1
        else:
            new_id = int(current_max) + 1
        
        # 更新最大id
        self.client.set('max_chat_id', new_id)
        
        return 'chat_' + str(new_id)
